package com.umeox.watch.moto.chat.ui

import android.content.Intent
import android.os.Bundle
import android.os.Environment
import android.provider.MediaStore
import android.widget.ImageView
import android.widget.Toast
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.RecyclerView
import com.umeox.moto.watch.themes.util.UToast
import com.umeox.moto.common.utils.FileUtils
import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity
import com.umeox.watch.moto.chat.R
import com.umeox.watch.moto.chat.adapter.AlbumAdapter
import com.umeox.watch.moto.chat.adapter.GalleryLayoutManager
import com.umeox.watch.moto.chat.adapter.ScaleTransformer
import com.umeox.watch.moto.chat.bean.Photo
import com.umeox.watch.moto.chat.utils.Constants
import java.io.File
import java.util.concurrent.Executors

/**
 * Created by Rambo 2021/8/24
 * Describe:
 */
class AlbumActivity : ThemeAppCompatActivity(), AlbumAdapter.ItemClickListener {


    private lateinit var mSendIv: ImageView
    private var mPhotos = ArrayList<Photo>()
    private lateinit var mAlbumListAdapter: AlbumAdapter
    private val TARGET_DIRECTORY: String = FileUtils.getAppGalleryCache()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.act_album)
        initView()
        initData()
    }

    private fun initView() {
        mSendIv = findViewById(R.id.iv_send)
        val albumRv: RecyclerView = findViewById(R.id.rv_album_list)
        mSendIv.setOnClickListener {
            if (mAlbumListAdapter.photosSelected.isNotEmpty()) {
                val data = Intent()
                data.putStringArrayListExtra(
                    Constants.EXTRA_SHARE_PHOTO,
                    mAlbumListAdapter.photoPaths
                )
                setResult(RESULT_OK, data)
            }
            finish()
        }

        mAlbumListAdapter = AlbumAdapter(
            applicationContext,
            this
        )
        (albumRv.itemAnimator as DefaultItemAnimator).supportsChangeAnimations = false
        albumRv.setHasFixedSize(true)
        albumRv.adapter = mAlbumListAdapter


        val galleryLayoutManager =
            GalleryLayoutManager(
                GalleryLayoutManager.HORIZONTAL
            )
        galleryLayoutManager.setItemTransformer(ScaleTransformer())
        galleryLayoutManager.attach(albumRv)

//        albumRv.layoutManager = GridLayoutManager(this, 3)
//        albumRv.addItemDecoration(GridDivider(this, 2, Color.TRANSPARENT))

    }


    private fun initData() {
        if (Environment.getExternalStorageState() != Environment.MEDIA_MOUNTED) {
            UToast.show("暂无外部存储", Toast.LENGTH_SHORT)
            return
        }
        Executors.newSingleThreadExecutor().execute {

            // 只查询jpeg和png的图片
            val mCursor = contentResolver.query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                null,
                MediaStore.Images.Media.MIME_TYPE + "=? or "
                        + MediaStore.Images.Media.MIME_TYPE + "=?",
                arrayOf("image/jpeg", "image/png"),
                MediaStore.Images.Media.DATE_MODIFIED + " desc"
            )
            if (mCursor != null) {
                mPhotos.clear()
                while (mCursor.moveToNext()) {
                    val path =
                        mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.DATA))
                    val file = File(path)
                    val absolutePath = file.absolutePath
                    if (absolutePath.startsWith(TARGET_DIRECTORY + File.separator + "IMG_")) {
                        val photo = Photo()
                        photo.path = path
                        mPhotos.add(photo)
                    }
                }
                mCursor.close()
                runOnUiThread { mAlbumListAdapter.setDatas(mPhotos) }
            }
        }
    }

    override fun onItemClicked(position: Int, photo: Photo) {
        when {
            mAlbumListAdapter.photosSelected.size < 9 -> {
                mAlbumListAdapter.addOrRemoveSelected(position)
            }
            mAlbumListAdapter.photosSelected.contains(photo) -> {
                mAlbumListAdapter.addOrRemoveSelected(position)
            }
            else -> {
                UToast.show(getString(R.string.str_pic_limit), Toast.LENGTH_SHORT)
            }
        }
    }
}